package com.hqyj.javaSpringBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.City;
import com.hqyj.javaSpringBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {
    @Autowired
    private CityService cityService;

    /*
    * 127.0.0.1/api/city -----post
    * {"cityName"="dream","localCityName"="梦幻之城","countryId"="522"}
    * */
    //consumes 设置前端返回得到的数据类型
    //RequestBody 接收的是请求体里面的数据；而RequestParam接收的是key-value里面的参数
    @PostMapping(value = "/city",consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }

    /*
    * 127.0.0.1/api/city -----put
    * cityName=shanghai,localCityName=上海,cityId=1890
    * */
    //@ModelAttribute从from表单或url地址中取值
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }

    /*
    * 127.0.0.1/api/city/2255 -----delete
    * */
    //@PathVariable是spring3.0的一个新功能：接收请求路径中占位符的值
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId){
        return cityService.deleteCityByCityId(cityId);
    }

    /*
    * 127.0.0.1/api/city/1890 -------get
    *
    * */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId){
        return cityService.getCityByCityId(cityId);
    }

    /*
    * 127.0.0.1/api/cities/522 -------get
    * */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * 127.0.0.1/api/cities ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/cities", consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1/api/batch/cities ---- post
     * [{"cityName"="dream","localCityName"="梦幻之城1","countryId"="521"}
     * ,{"cityName"="dream","localCityName"="梦幻之城2","countryId"="522"}]
     */
    @PostMapping(value="/batch/cities",consumes = "application/json")
    public Result<Object> insertCities(@RequestBody List<City> cities){
        return cityService.insertCities(cities);

    }
}
